import thin_render from "./thin_render.mjs";
import { nearest_querycontainer } from "./nearest.mjs";
import thin_rerender from "./thin_rerender.mjs";

thin_rerender;
export default function pager_template(container, template) {
  // console.log({ container, template });
  let pager = document.createElement("pager");
  container.appendChild(pager);

  if (template.pager === true) {
    container.thin_data.totalpage = Math.ceil(
      container.thin_data.totalrecord / container.thin_data.pagesize
    );
    thin_render(pager, [
      {
        prevpage: {
          a: "<<",
          click: (p) => {
            // console.log({ prevpage: p });
            let query_container = nearest_querycontainer(container);
            query_container.thin_query.pagenum = p.org_data.pagenum - 1;
            thin_rerender(container);
          },
        },
        when: (p) => {
          // console.log(p);
          return p.data.pagenum > 1;
        },
      },
      " [[pagenum]] / [[totalpage]] pages [[totalrecord]] records ",
      {
        nextpage: {
          a: ">>",
          click: (p) => {
            // console.log({ nextpage: p });
            let query_container = nearest_querycontainer(container);
            query_container.thin_query.pagenum = p.org_data.pagenum + 1;
            thin_rerender(container);
          },
        },
        when: (p) => {
          // console.log(p);
          return p.data.pagenum < p.data.totalpage;
        },
      },
    ]);
  }
}
